package se.no.noko.engine.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import se.no.noko.engine.NokoApplication;
import android.content.res.Resources;

public class FileLoader {

	private static InputStream getInputStreamFromResource(int resourceId) {
		Resources resources = NokoApplication.getAppContext().getResources();
		String resourceName = resources.getResourceName(resourceId);
		return resources.openRawResource(resources.getIdentifier(resourceName,
				null, null));
	}

	public static String loadPlainText(int resourceId) {
		InputStream stream = null;
		StringBuilder builder = new StringBuilder();
		try {
			stream = getInputStreamFromResource(resourceId);
			BufferedReader buffer = new BufferedReader(new InputStreamReader(
					stream));
			String line = null;
			while ((line = buffer.readLine()) != null) {
				builder.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stream != null) {
					stream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return builder.toString();
	}
}
